<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html;charset=utf-8"	pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
	<link type="text/css" rel="StyleSheet" href=""<%=basePath%>css/home.css"></link>
	<link style="text/css" rel="StyleSheet" href="<%=basePath%>css/main.css"></link>
	<script type="text/javascript">
	var pageList = new Array();
	var content = new Array();
	var splitStr = "-_-"; 
	
	var bookName = "";
	var saveDir = "";
	var bookIndexURL = "";
	var suffix = "";
	var sourceIndex = 0;
	var ebookArr = new Array();
	var filtersArr = new Array();
	var errorCount = 0;	
	var filterStr = "";
	
	function init(){
		initAllEBook();
		initCategoryURLs();
	}
	
	function initAllEBook(){
		//[0]:distance,[1]:urlPrefix,[2]:urlStartFilter,
		//[3]:urlEndFilter,[4]:source,[5]:Class Name,[6]:base url,
		var qqbook = new Array(0,"","javascript:opennew('","'","QQ","QQEBook","book.qq.com");
		var wwbook = new Array(12,"http://book.55www.com","<p class=\"art_","\">","WWEBook","WW","book.55www.com");
		
		ebookArr[0] = qqbook;
		ebookArr[1] = wwbook;
		
		
		var qqFilter = new Array();
			//[0]:文章中的哪部分,[1]:tabStart,[2]:tabFixed,[3]:tabEnd
			qqFilter[0] = new Array("title","<h1>","<h1>","</h1>");
			qqFilter[1] = new Array("content","<div id=\"content\"", "<div", "</div>");
		var wwFilter = new Array();
			wwFilter[0] = new Array("title","<h1 id=\"arttitle\">","<h1>","</h1>");
			wwFilter[1] = new Array("content","<div id=\"artcontent\">","<div","</div>");
		
		filtersArr[0] = qqFilter;
		filtersArr[1] = wwFilter;
	}
	
	function initCategoryURLs(){
		var selectEBook = ebookArr[sourceIndex];
		var filterEBook = filtersArr[sourceIndex];		
		for(var i=0;i<filterEBook.length;i++){
			if(i==0){
				filterStr = filterEBook[i] + "";
			}else{
				filterStr += "--" + filterEBook[i] + "";
			}
		}

		if(bookIndexURL.indexOf(selectEBook[6])<0){
			alert("请选择正确的书库来源，或者本系统当前还未支持您指定的地址！\n\r若愿意，您可以把该地址提交到系统中，我们会尽快为您将该来源加到系统书库来源中。");
			return;
		}
		
		document.getElementById("divGetEBookMessage").innerHTML += "电子书目录初始化：获取各章节URL地址<br>";
		var url = bookIndexURL;
		var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		xmlHttp.open("GET","initCategoryUrls.jsp?url="+url+"&distance=" + selectEBook[0] + "&urlPrefix=" + selectEBook[1] + "&urlStartFilter=" + selectEBook[2] + "&urlEndFilter=" + selectEBook[3] + "&source=" + selectEBook[4],false);
		xmlHttp.onreadystatechange = function(){
				if(xmlHttp.readyState == 4){
					var text = xmlHttp.responseTEXT;
					if(text.indexOf(splitStr)>0){
						pageList = text.split(splitStr);						
						checkCategoryURLs();
					}else{
						document.getElementById("divGetEBookMessage").innerHTML += text + "<br>";
					}					
				}
			}
		xmlHttp.send(null);
	}
	
	function checkCategoryURLs(){
		if(pageList.length<=0){
			document.getElementById("divGetEBookMessage").innerHTML += "没有获取到电子书目录，请确认是否支持该网站！<br>";
			document.getElementById("divGetEBookMessage").innerHTML += "失败可能原因：1。暂不支持所找的网站 2.首页的URL地址不是目录页。<br>"
		}else{
			document.getElementById("divGetEBookMessage").innerHTML += "共有 " + pageList.length + " 页需要提取。<br>"
			
			document.getElementById("divGetEBookMessage").innerHTML += "正在提取第 1 页的内容:" + pageList[0];
			getPageContentByIndex(0,pageList[0]);
		}
	}
	
	function getPageContentByIndex(pageNo,pageUrl){			
			var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			xmlHttp.open("GET","getPageContentByIndex.jsp?url="+pageUrl+"&filters="+filterStr,false);
			xmlHttp.onreadystatechange = function(){
					if(xmlHttp.readyState == 4){
						var text = xmlHttp.responseTEXT;
						if(text.indexOf(":(error):")<0){
							content[pageNo] = text;					
							document.getElementById("divGetEBookMessage").innerHTML += " ---> 第 " + (pageNo+1) + " 页提取完毕";
						}else{
							errorCount ++;
							content[pageNo] = "";
							document.getElementById("divGetEBookMessage").innerHTML += "<font color='red'> ---> 第 " + (pageNo+1) + " 页提取失败。</font>";
						}
					}
				}
			xmlHttp.send(null);	
		
		if(pageNo<pageList.length-2){
			document.getElementById("divGetEBookMessage").innerHTML += "<br>正在提取第 " + (pageNo + 2) + " 页的内容:" + pageList[pageNo + 1];
			document.getElementById("divGetEBookMessage").scrollTop = document.getElementById("divGetEBookMessage").scrollHeight;
			setTimeout("getPageContentByIndex(" + (pageNo + 1) + ",'" + pageList[pageNo + 1] + "')",100);
		}else{
			window.setTimeout("writeDataToFile()",1000);
		}
	}
	
	function writeDataToFile(){
		document.getElementById("divGetEBookMessage").innerHTML += "<br>提取结束,将提取的数据读入文件...";
		document.getElementById("divGetEBookMessage").scrollTop = document.getElementById("divGetEBookMessage").scrollHeight;
		var fso = new ActiveXObject("Scripting.FileSystemObject");
		var f = fso.createTextFile(saveDir + bookName + "." + suffix,2,true);
		for(var i=0;i<content.length;i++)
			f.write(content[i]);
		f.close();
		finish();
	}
	
	function finish(){
		document.getElementById("divGetEBookMessage").innerHTML += "<br>文件已完成,生成电子书结束";
		document.getElementById("divGetEBookMessage").innerHTML += "<br>文件存放地址：" + saveDir + bookName + "." + suffix;
		if(errorCount>0){
			document.getElementById("divGetEBookMessage").innerHTML += "<br>---------------------------------------------------"
			document.getElementById("divGetEBookMessage").innerHTML += "<br>共有 <font color='red'>" + errorCount + "</font> 个页面连接失败，失败原因可能是：";
			document.getElementById("divGetEBookMessage").innerHTML += "<br>&nbsp;&nbsp;&nbsp;&nbsp;连接有错误，或文件已不存在，或因网速原因导致连接不上";
		}
		document.getElementById("divGetEBookMessage").scrollTop = document.getElementById("divGetEBookMessage").scrollHeight;
	}
	
	function startSave(){
		bookName = document.getElementById("input_bookname").value;
		saveDir = document.getElementById("input_bookdir").value;
		bookIndexURL = document.getElementById("input_bookIndex").value;
		suffix = document.getElementById("select_suffix").value;
		sourceIndex = document.getElementById("select_sourceWebsite").value;
		
		if(bookName=="" || saveDir=="" || bookIndexURL=="" || sourceIndex==""){
			alert("所有输入内容都不能为空！");
			return;
		}
		init();
	}
	</script>
  </head>
  
  <body topmargin="0px" leftmargin="0px" bottommargin="0px" rightmargin="0px">
<div class="top"></div>
    <div class="middle">		
	<div class="content">
	<div class="title">在线提取电子书工具</div>
	<table width="100%" style="font-family: 宋体; font-size: 12px; color: #8a8a79;" cellpadding="2" cellspacing="2">
		<tr>
			<td width="160">电子书首页地址【带目录】：</td><td><input id="input_bookIndex" size="60" type="text" value="http://book.qq.com/s/book/0/19/19535/index.shtml"></td>
		</tr>
		<tr>
			<td>另存为名称：</td><td><input id="input_bookname" type="text" size="60" value="如何让孩子吃得安全又营养"></td>
		</tr>
		<tr>
			<td>
				存放地址【本地文件夹】：</td><td><input id="input_bookdir" size="40" type="text" value="C:\\">
				文件格式：<select id="select_suffix"><option value="txt">TXT</option><option value="doc">DOC</option></select>
			</td>
		</tr>
		<tr>
			<td>当前支持的书库来源：</td>
			<td>
				<select id="select_sourceWebsite">
					<option value="">选择来源</option>
					<option selected value="0">腾讯QQ读书</option>
					<option value="1">五五武侠小说</option>
				</select>			
			<input type="button" value="生成电子书" onclick="startSave()"></td>
		</tr>
		<tr>
			<td colspan="2" style="padding-left:5px;">帮助：<a href="#">请求增加其他书库来源</a></td>	
		</tr>
	</table><br>
    <div id="divGetEBookMessage" style="width:100%; overflow:auto; border:#C5C5C5 solid 1px; height:350px; padding:10px; background-color:#5A5A5A;font-family: 宋体; font-size: 12px; color: #8a8a79;"></div>
	</div>
</div>
</body>
</html>
